package com.arena;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.arena.model.Question;
import com.arena.model.Questions;

public class MainActivity extends Activity {
	Questions quest;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    	// create question collection
        TextView tv = (TextView) findViewById(R.id.tvPresent);
    	quest = createQuestion(tv);
    	
    }
    
    /**
     * Create question collection which fetch date from server
     * @param tv
     * @return
     */
    private Questions createQuestion(final TextView tv) {    	
    	Questions qs = new Questions() {
    		@Override
    		public void onPostExecute(String result) {    		
    			super.onPostExecute(result);
    			Log.i("MainActivity", "PostExecute");
    			String text = "";
    			text += getDate(); 
    			Question[] qList = this.getQuestionList();
    			for (int i = 0; i < qList.length; i++) {
					text += qList[i].getQuestion();
					text += qList[i].getAnswersString();					
				}
    			tv.setText(text);
    		}
    	};
		return qs;
	}
    /**
     * Get data from server, this is onClick function of GetDataButton
     * @param v
     */
	public void getData(View v) {
    	quest.fetch();
    }
}
